package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.FpFaUserDTO;
import com.financing.platform.service.IFpFaUserService;
import com.financing.platform.util.DTOdateSet;

@Controller("fpFaUserController")
@RequestMapping("/api/fpFaUser")
public class FpFaUserController extends BaseAction { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpFaUserService fpFaUserService;
	
	@RequestMapping("/listFpFaUser")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpFaUserDTO fpFaUser,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpFaUser list begin .......");
			
			fpFaUser.setBegin(pager.getStartIndex());
			fpFaUser.setPageSize(pager.getPageSize());
			
			int count = fpFaUserService.queryListCount(fpFaUser);
			
			pager.setTotalCount(count);
			
			List<FpFaUserDTO> fpFaUserList = null;
			
			if(count > 0){
				fpFaUserList = fpFaUserService.queryList(fpFaUser);
			}else{
				fpFaUserList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpFaUserList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpFaUser list end .......");
		}catch(Exception e){
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpFaUser")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpFaUserDTO fpFaUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpFaUser  begin .......");
			
			DTOdateSet.setCreateInfo(fpFaUser,request.getSession());
			DTOdateSet.setUpdateInfo(fpFaUser,request.getSession());
			
			int id = fpFaUserService.insert(fpFaUser);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpFaUser list end .......");
		}catch(Exception e){
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpFaUser")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpFaUserDTO fpFaUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpFaUser  begin .......");
			
			Integer id = fpFaUser.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpFaUserDTO dto = fpFaUserService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpFaUser  end .......");
		}catch(Exception e){
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpFaUser")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpFaUserDTO fpFaUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpFaUser  begin .......");
			
			Integer id = fpFaUser.getId();
			
			
			fpFaUserService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpFaUser  end .......");
		}catch(Exception e){
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpFaUser")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpFaUserDTO fpFaUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpFaUser  begin .......");
			
			DTOdateSet.setUpdateInfo(fpFaUser,request.getSession());
			
			fpFaUserService.update(fpFaUser);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpFaUser list end .......");
		}catch(Exception e){
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpFaUser")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpFaUserDTO fpFaUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpFaUser  begin .......");
			
			
			DTOdateSet.setUpdateInfo(fpFaUser,request.getSession());
			
			fpFaUserService.updateForce(fpFaUser);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpFaUser list end .......");
		}catch(Exception e){
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
